import { ForbiddenException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Banner } from './banner.entity';
import { Video } from '../video/video.entity';

const { getDate } = require('../utils/tool');
const { updateUserError } = require('../provider/error');

@Injectable()
export class BannerService {
  constructor(
    @InjectRepository(Banner) private banner: Repository<Banner>,
    @InjectRepository(Video) private video: Repository<Video>,
  ) {}

  // 获取banner列表
  async getBannerList() {
    const bannerList: any = await this.banner
      .createQueryBuilder('banner')
      .getMany();
    for (const t of bannerList) {
      const index: number = bannerList.indexOf(t);
      bannerList[index].video = await this.video.findOne({ id: t.videoId });
    }
    return bannerList;
  }

  //  添加轮播图
  async addBanner(bodyData) {
    const { bannerTitle, bannerImage, videoId, bannerDescribe } = bodyData;
    if (!bannerTitle)
      throw new ForbiddenException({
        message: '轮播图标题 bannerTitle 不能为空!',
        code: 1,
      });
    if (!bannerImage)
      throw new ForbiddenException({
        message: '轮播图片 bannerImage 不能为空!',
        code: 1,
      });
    if (!videoId)
      throw new ForbiddenException({
        message: '视频itemID videoId 不能为空!',
        code: 1,
      });
    const videoResults = await this.video.findOne({ id: videoId });
    if (!videoResults)
      throw new ForbiddenException({
        message: '关联是视频不存在!',
        code: 1,
      });
    try {
      await this.banner.save({
        bannerDescribe: bannerDescribe ? bannerDescribe : '',
        bannerTitle,
        bannerImage,
        videoId,
        create_time: new Date(),
        updated_time: new Date(),
      });
      return [];
    } catch (e) {
      console.log('error', e);
      throw new ForbiddenException({
        message: '添加轮播图失败!',
        code: 1,
      });
    }
  }

  //  修改轮播图
  async editBanner(bodyData) {
    const { id, bannerTitle, bannerImage, videoId, bannerDescribe } = bodyData;
    if (!id)
      throw new ForbiddenException({
        message: '轮播图ID 不能为空!',
        code: 1,
      });
    if (!bannerTitle)
      throw new ForbiddenException({
        message: '轮播图标题 bannerTitle 不能为空!',
        code: 1,
      });
    if (!bannerImage)
      throw new ForbiddenException({
        message: '轮播图片 bannerImage 不能为空!',
        code: 1,
      });
    if (!videoId)
      throw new ForbiddenException({
        message: '视频itemID videoId 不能为空!',
        code: 1,
      });
    const videoResults = await this.video.findOne({ id: videoId });
    if (!videoResults)
      throw new ForbiddenException({
        message: '关联是视频不存在!',
        code: 1,
      });
    try {
      await this.banner.update(id, {
        bannerDescribe: bannerDescribe ? bannerDescribe : '',
        bannerTitle,
        bannerImage,
        videoId,
        updated_time: new Date(),
      });
      return [];
    } catch (e) {
      console.log('error', e);
      throw new ForbiddenException({
        message: '添加轮播图失败!',
        code: 1,
      });
    }
  }

  //  删除轮播图
  deleteBanner(params) {
    if (!params.id)
      throw new ForbiddenException({
        message: '轮播图ID 不能为空!',
        code: 1,
      });
    try {
      this.banner.delete(params.id);
      return [];
    } catch (e) {
      console.log('error', e);
    }
  }
}
